/**
 * Copyright(c) 2013-2013 by Puhuifinance Inc.
 * All Rights Reserved
 */
package com.puhui.chome.util;

import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * jackjson一些转换方法
 * 
 * @author little
 */
public class JsonTools {

    private JsonTools() {
    }

    private static final Logger logger = LoggerFactory.getLogger(JsonTools.class);

    /**
     * json转换为java对象
     * 
     * <pre>
     * return JackJson.fromJsonToObject(this.answersJson, JackJson.class);
     * </pre>
     * 
     * @param <T>
     *            要转换的对象
     * @param json
     *            字符串
     * @param valueType
     *            对象的class
     * @return 返回对象
     */
    public static <T> T fromJsonToObject(String json, Class<T> valueType) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.readValue(json, valueType);
        } catch (Exception e) {
            logger.error("Exception: ", e);
        }
        return null;
    }

    /**
     * java对象转换为json字符串
     * 
     * @param object
     *            Java对象
     * @return 返回字符串
     */
    public static String fromObjectToJson(Object object) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(object);
        } catch (Exception e) {
            logger.error("Exception: ", e);
        }
        return null;
    }

}
